package net.tv90.bfmccore.command;

import net.tv90.bfmccore.slot.SlotHelper;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.io.IOException;
import java.util.concurrent.atomic.LongAdder;

import static net.tv90.bfmccore.helper.FactionHelper.getFaction2;

public class SlotCommand implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (label.equalsIgnoreCase("slot")) {
            if (args.length == 0) {
                sender.sendMessage("/slot saveweapon <主/副武器> <枪械ID> <弹药种类> <弹药数量> <玩家>");
                sender.sendMessage("/slot saveother <近战武器/服装> <物品ID/服装编号> <玩家>");
                sender.sendMessage("/slot saveattachment <主/副武器> <配件类型> <配件ID> <玩家>");
                sender.sendMessage("/slot saveammo <主/副武器> <弹药ID> <弹药数量> <玩家>");
                sender.sendMessage("/slot saveammotype <主/副武器> <弹药ID> <玩家>");
                sender.sendMessage("/slot savegrenade <物品ID> <数量> <玩家>");
                sender.sendMessage("/slot savegadgets <物品ID> <数量> <物品栏索引> <玩家>");
                sender.sendMessage("/slot savegadgets2 <物品ID> <数量> <物品栏索引> <玩家>");
                sender.sendMessage("/slot cleargadgets <玩家> 清空gadgets列表");
                sender.sendMessage("/slot cleargadgets2 <玩家> 清空gadgets2列表");
                sender.sendMessage("/slot give <玩家> 根据玩家所在阵营和兵种给予玩家背包内的物品");
                sender.sendMessage("/slot open <武器种类> <玩家> 为玩家打开特定背包选择菜单");
                sender.sendMessage("例如/slot saveweapon MainWeapon FLANSMOD_AK74M FLANSMOD_545X3930RDMAG 6");
                sender.sendMessage("例如/slot saveweapon SideArms FLANSMOD_COMPACT45 FLANSMOD_45ACP8RDMAG 6 TV90");
                sender.sendMessage("例如/slot saveother MeleeWeapon FLANSMOD_ACB90");
                sender.sendMessage("例如/slot saveother Cloth RUASS");
                sender.sendMessage("例如/slot saveattachment MainWeapon Scope FLANSMOD_COYOTE");
                sender.sendMessage("例如/slot saveattachment SideArms Barrel FLANSMOD_9MMSD");
                sender.sendMessage("例如/slot savegadgets FLANSMOD_BANDAGE 1 8");
                sender.sendMessage("例如/slot savegrenade FLANSMOD_M67 2");

            }
            if (args[0].equalsIgnoreCase("dbg")) {
                System.out.println(SlotHelper.DefaultSlotData);
            }
            if (args[0].equalsIgnoreCase("saveweapon")) {
                String type = args[1];
                String gun = args[2];
                String ammo = args[3];
                int amount = Integer.parseInt(args[4]);
                Player player = Bukkit.getPlayerExact(args[5]);
                try {
                    SlotHelper.saveWeapon(player, type, gun, ammo, amount);
                    sender.sendMessage("已保存");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (args[0].equalsIgnoreCase("saveammo")) {
                String type = args[1];
                String ammo = args[2];
                int amount = Integer.parseInt(args[3]);
                Player player = Bukkit.getPlayerExact(args[4]);
                try {
                    SlotHelper.saveAmmo(player, type, ammo, amount);
                    sender.sendMessage("已保存");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (args[0].equalsIgnoreCase("saveammotype")) {
                String type = args[1];
                String ammo = args[2];
                Player player = Bukkit.getPlayerExact(args[3]);
                try {
                    SlotHelper.saveAmmoType(player, type, ammo);
                    sender.sendMessage("已保存");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (args[0].equalsIgnoreCase("saveother")) {
                String type = args[1];
                String gun = args[2];
                Player player = Bukkit.getPlayerExact(args[3]);
                try {
                    SlotHelper.saveOther(player, type, gun);
                    sender.sendMessage("已保存");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (args[0].equalsIgnoreCase("saveattachment")) {
                String guntype = args[1];
                String atttype = args[2];
                String att = args[3];
                Player player = Bukkit.getPlayerExact(args[4]);
                try {
                    SlotHelper.saveAttachments(player, guntype, atttype, att);
                    sender.sendMessage("已保存");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (args[0].equalsIgnoreCase("savegrenade")) {
                String grenade = args[1];
                int num = Integer.parseInt(args[2]);
                Player player = Bukkit.getPlayerExact(args[3]);
                try {
                    SlotHelper.saveGrenade(player, grenade, num);
                    sender.sendMessage("已保存");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (args[0].equalsIgnoreCase("savegadgets")) {
                String item = args[1];
                int num = Integer.parseInt(args[2]);
                int index = Integer.parseInt(args[3]);
                Player player = Bukkit.getPlayerExact(args[4]);
                try {
                    SlotHelper.saveGadgets(player, item, num, index);
                    sender.sendMessage("已保存");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (args[0].equalsIgnoreCase("savegadgets2")) {
                String item = args[1];
                int num = Integer.parseInt(args[2]);
                int index = Integer.parseInt(args[3]);
                Player player = Bukkit.getPlayerExact(args[4]);
                try {
                    SlotHelper.saveGadgets2(player, item, num, index);
                    sender.sendMessage("已保存");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (args[0].equalsIgnoreCase("give")) {
                Player player = Bukkit.getPlayerExact(args[1]);
                try {
                    SlotHelper.giveSlot(player);
                    System.out.println(getFaction2(player));
                    sender.sendMessage("已给予");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (args[0].equalsIgnoreCase("cleargadgets")) {
                Player player = Bukkit.getPlayerExact(args[1]);
                try {
                    SlotHelper.clearGadgets(player);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (args[0].equalsIgnoreCase("cleargadgets2")) {
                Player player = Bukkit.getPlayerExact(args[1]);
                try {
                    SlotHelper.clearGadgets2(player);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (args[0].equalsIgnoreCase("open")) {
                String type = args[1];
                Player player = Bukkit.getPlayerExact(args[2]);
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"cc open " + getFaction2(player) + "_" + type + " " + player.getName());
            }
            return true;
        }
        return true;
    }
}
